package com.yeyks.commonReference.service.vip.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.account.dal.dao.AccountInfoMapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.em.vip.VipCommissionRateConfigEnum;
import com.yeyks.commonReference.service.vip.VipCommissionRateConfigService;
import com.yeyks.vip.dal.domain.VipCommissionRateConfig;
import com.yeyks.vip.dal.dao.VipCommissionRateConfigMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * vip分佣比例配置表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-08-22
 */
@Slf4j
@Service
public class VipCommissionRateConfigServiceImpl extends ServiceImpl<VipCommissionRateConfigMapper, VipCommissionRateConfig> implements VipCommissionRateConfigService {


    @Autowired
    private AccountInfoMapper accountInfoMapper;

    /**
     * 通过账户id和类型查询对应的比率
     *
     * @param accountId 账户id
     * @param type      类型
     * @param vipType   vip类型，1大V,2小V
     */
    @Override
    public VipCommissionRateConfig selectByAccountIdAndType(Integer accountId, Integer type, Integer vipType) {
        return baseMapper.selectOne(new QueryWrapper<VipCommissionRateConfig>()
                .eq(VipCommissionRateConfig.ACCOUNT_ID, accountId)
                .eq(VipCommissionRateConfig.TYPE, type)
                .eq(VipCommissionRateConfig.VIP_TYPE, vipType)
                .last("limit 1"));
    }

    @Override
    public Boolean addVipCommissionRateConfig(Integer accountId,Integer vipType) {
        try {
            List<Integer> typeIds = convertType();
            List<VipCommissionRateConfig> vipCommissionRateConfigList = Lists.newArrayList();

            //执行循环添加比例信息
            if (!CollectionUtils.isEmpty(typeIds)) {
                VipCommissionRateConfig commissionRateConfig = null;
                Date date = new Date();
                for (Integer type : typeIds) {
                    commissionRateConfig = new VipCommissionRateConfig();
                    commissionRateConfig.setAccountId(accountId);
                    commissionRateConfig.setRateValue(VipCommissionRateConfigEnum.getRateValueByType(type));
                    commissionRateConfig.setCreateTime(date);
                    //VIPType 的相关判断的值
                    commissionRateConfig.setVipType(vipType);
                    commissionRateConfig.setType(type);
                    vipCommissionRateConfigList.add(commissionRateConfig);
                }
            }
            return baseMapper.insertByBatch(vipCommissionRateConfigList) == typeIds.size();
        } catch (Exception e) {
            log.info("插入vip账户佣金比率异常accountId = {}", accountId);
        }
        return false;
    }

    /**
     * 得到添加的类型列表Id
     *
     * @return 返回结果
     */
    private List<Integer> convertType() {
        List<Integer> typeIds = Lists.newArrayList();
        List<VipCommissionRateConfigEnum> rateConfigEnums = Arrays.asList(VipCommissionRateConfigEnum.values());
        rateConfigEnums.forEach(vipCommissionRateConfigEnum -> {
            typeIds.add(vipCommissionRateConfigEnum.getType());
        });
        return typeIds;
    }
}
